<template>
  <div class="pdf-viewer-example">
    <div class="control-panel">
      <h3>PDF查看器配置示例</h3>
      
      <div class="control-group">
        <label>PDF文件:</label>
        <input 
          v-model="pdfSrc" 
          type="text" 
          placeholder="输入PDF文件路径"
          class="input-field"
        >
        <button @click="loadEmptyPdf" class="test-btn">测试空路径</button>
        <button @click="loadInvalidPdf" class="test-btn">测试无效路径</button>
      </div>

      <div class="control-group">
        <label>按钮控制:</label>
        <div class="checkbox-group">
          <label class="checkbox-label">
            <input v-model="showDownload" type="checkbox">
            显示下载按钮
          </label>
          <label class="checkbox-label">
            <input v-model="showPrint" type="checkbox">
            显示打印按钮
          </label>
          <label class="checkbox-label">
            <input v-model="showFullscreen" type="checkbox">
            显示全屏按钮
          </label>
          <label class="checkbox-label">
            <input v-model="showRetry" type="checkbox">
            显示重试按钮
          </label>
        </div>
      </div>

      <div class="control-group">
        <label>主题配置:</label>
        <div class="color-group">
          <div class="color-item">
            <label>工具栏背景:</label>
            <input v-model="toolbarBg" type="color">
          </div>
          <div class="color-item">
            <label>工具栏文字:</label>
            <input v-model="toolbarText" type="color">
          </div>
          <div class="color-item">
            <label>内容背景:</label>
            <input v-model="contentBg" type="color">
          </div>
          <div class="color-item">
            <label>主题色:</label>
            <input v-model="primaryColor" type="color">
          </div>
        </div>
      </div>

      <div class="preset-themes">
        <button @click="applyDarkTheme" class="theme-btn">深色主题</button>
        <button @click="applyLightTheme" class="theme-btn">浅色主题</button>
        <button @click="applyBlueTheme" class="theme-btn">蓝色主题</button>
      </div>
    </div>

    <div class="pdf-container">
      <PdfWithThumbnails
        :src="pdfSrc"
        :show-download-btn="showDownload"
        :show-print-btn="showPrint"
        :show-fullscreen-btn="showFullscreen"
        :show-retry-btn="showRetry"
        :toolbar-background-color="toolbarBg"
        :toolbar-text-color="toolbarText"
        :content-background-color="contentBg"
        :thumbnail-background-color="thumbnailBg"
        :thumbnail-item-background-color="thumbnailItemBg"
        :primary-color="primaryColor"
        :button-hover-color="buttonHover"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import PdfWithThumbnails from './PdfWithThumbnails.vue';

/**
 * PDF查看器使用示例组件
 * @author Ning,Li
 */

// PDF源文件路径
const pdfSrc = ref('/test1.pdf');

// 按钮显示控制
const showDownload = ref(true);
const showPrint = ref(true);
const showFullscreen = ref(true);
const showRetry = ref(true);

// 主题颜色配置
const toolbarBg = ref('#333333');
const toolbarText = ref('#ffffff');
const contentBg = ref('#222222');
const primaryColor = ref('#42b983');

// 计算衍生颜色
const thumbnailBg = computed(() => {
  const color = contentBg.value;
  // 让缩略图背景稍微亮一点
  return adjustBrightness(color, 0.1);
});

const thumbnailItemBg = computed(() => {
  return '#f5f5f5';
});

const buttonHover = computed(() => {
  const color = toolbarBg.value;
  return adjustBrightness(color, 0.2);
});

/**
 * 调整颜色亮度
 * @author Ning,Li
 * @param {string} hex - 十六进制颜色值
 * @param {number} factor - 调整因子（正数变亮，负数变暗）
 * @returns {string} 调整后的颜色值
 */
function adjustBrightness(hex, factor) {
  // 移除#号
  hex = hex.replace('#', '');
  
  // 转换为RGB
  const r = parseInt(hex.substr(0, 2), 16);
  const g = parseInt(hex.substr(2, 2), 16);
  const b = parseInt(hex.substr(4, 2), 16);
  
  // 调整亮度
  const newR = Math.max(0, Math.min(255, Math.round(r + (255 - r) * factor)));
  const newG = Math.max(0, Math.min(255, Math.round(g + (255 - g) * factor)));
  const newB = Math.max(0, Math.min(255, Math.round(b + (255 - b) * factor)));
  
  // 转换回十六进制
  return `#${newR.toString(16).padStart(2, '0')}${newG.toString(16).padStart(2, '0')}${newB.toString(16).padStart(2, '0')}`;
}

/**
 * 测试空PDF路径
 * @author Ning,Li
 */
const loadEmptyPdf = () => {
  pdfSrc.value = '';
};

/**
 * 测试无效PDF路径
 * @author Ning,Li
 */
const loadInvalidPdf = () => {
  pdfSrc.value = '/invalid-file.pdf';
};

/**
 * 应用深色主题
 * @author Ning,Li
 */
const applyDarkTheme = () => {
  toolbarBg.value = '#1a1a1a';
  toolbarText.value = '#ffffff';
  contentBg.value = '#0f0f0f';
  primaryColor.value = '#4fc3f7';
};

/**
 * 应用浅色主题
 * @author Ning,Li
 */
const applyLightTheme = () => {
  toolbarBg.value = '#f5f5f5';
  toolbarText.value = '#333333';
  contentBg.value = '#ffffff';
  primaryColor.value = '#2196f3';
};

/**
 * 应用蓝色主题
 * @author Ning,Li
 */
const applyBlueTheme = () => {
  toolbarBg.value = '#1976d2';
  toolbarText.value = '#ffffff';
  contentBg.value = '#e3f2fd';
  primaryColor.value = '#ff5722';
};
</script>

<style scoped>
.pdf-viewer-example {
  display: flex;
  height: 100vh;
  background: #f0f0f0;
}

.control-panel {
  width: 350px;
  background: #fff;
  padding: 20px;
  border-right: 1px solid #ddd;
  overflow-y: auto;
  box-shadow: 2px 0 5px rgba(0,0,0,0.1);
}

.control-panel h3 {
  margin: 0 0 20px 0;
  color: #333;
  font-weight: 600;
}

.control-group {
  margin-bottom: 25px;
}

.control-group label {
  display: block;
  font-weight: 500;
  color: #555;
  margin-bottom: 8px;
}

.input-field {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  margin-bottom: 8px;
}

.test-btn {
  background: #ff9800;
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  margin-right: 8px;
  transition: background 0.2s;
}

.test-btn:hover {
  background: #f57c00;
}

.checkbox-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-weight: normal;
  margin-bottom: 0;
}

.checkbox-label input {
  margin-right: 8px;
}

.color-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.color-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.color-item label {
  font-size: 12px;
  margin-bottom: 0;
}

.color-item input[type="color"] {
  width: 100%;
  height: 32px;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
}

.preset-themes {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.theme-btn {
  background: #607d8b;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.theme-btn:hover {
  background: #455a64;
  transform: translateY(-1px);
}

.pdf-container {
  flex: 1;
  background: #222;
}
</style> 